<template>
	
	
</template>

<script>
import service_enter from '@/service/enter.js'

export default {
	data() {
		return {
			
		}
	},
	
	onLoad() {
		// testcode
		// this.getEnterStatus()
		
		this.getToken()
	},
	
	methods: {
		getToken() {
			let token = this.$store.state.index.token
			let tokenTime = this.$store.state.index.tokenTime
			let isBindMobile = this.$store.state.index.isBindMobile
			let userMobile = this.$store.state.index.userMobile
			
			// 2小时以内
			if (token && Date.now() - tokenTime < 7200000) {  // 7200000: 2小时
				// 已绑定手机号 => 获取用户入驻信息
				if (isBindMobile) {
					this.getEnterStatus(userMobile)
				}
				// 未绑定手机号 => 初始用户 => 跳转入驻页面
				else {
					uni.redirectTo({ url: "/pages/enter/index/index"})
				}
			}
			// 2小时以后 => 重新获取token
			else {
				this.$utils.showLoading()
				this.$utils.login({
					success: (res) => {
						service_enter.getToken({ code: res.code })
							.then(res => {
								if (res.code == 1) {
									let data = res.data
									let isBindMobile = data.bind_mobile_status == 1 ? true : false   // 用户是否绑定手机号
									let token = data.token.access_token
									let userId = data.user_id
									let userMobile = data.mobile
									
									// 保存数据
									this.$store.commit('index/SET_token', token)
									this.$store.commit('index/SET_tokenTime', Date.now())
									this.$store.commit('index/SET_isBindMobile', isBindMobile)
									this.$store.commit('index/SET_userMobile', userMobile)
									this.$store.commit('index/SET_userId', userId)
									
									// 已绑定手机号 => 获取用户入驻信息
									if (isBindMobile) {
										this.getEnterStatus(userMobile)
									}
									// 未绑定手机号 => 初始用户 => 跳转入驻页面
									else {
										this.$utils.hideLoading()
										uni.redirectTo({ url: "/pages/enter/index/index"})
									}
								}
								else {
									this.$utils.hideLoading()
									this.$utils.showErr({ title: res.msg })
								}
							})
							.catch(err => {
								this.$utils.hideLoading()
								this.$utils.showErr()
							}) 
					}
				})
			}
		},
		
		// 获取入驻状态
		getEnterStatus(userMobile) {
			// testcode
			userMobile = '18819464791'
			
			
			let enterStatus = this.$store.state.index.enterStatus
			
			if (enterStatus) {
				// 已入驻 => 产品库页面
				if (enterStatus == 1) {
					uni.switchTab({ url: "/pages/mall/index/index" })
				}
				// 未入驻 => 入驻页面
				else {
					uni.redirectTo({ url: "/pages/enter/index/index"})
				}
			}
			else {
				service_enter.getEnterStatus({ mobile: userMobile })
					.then(res => {
						this.$utils.hideLoading()
						
						if (res.code == 1) {
							let data = res.data
							let enterStatus = data.status      // 入驻状态
							let enterRole = data.role          // 入驻角色
							let enterMobile = data.mobile      // 入驻手机号
							
							// 保存数据
							this.$store.commit("index/SET_enterStatus", enterStatus)
							this.$store.commit("index/SET_enterRole", enterRole)
							this.$store.commit("index/SET_enterMobile", enterMobile)
							
							// 已入驻 => 产品库页面
							if (enterStatus == 1) {
								uni.switchTab({ url: "/pages/mall/index/index" })
							}
							// 未入驻 => 入驻页面
							else {
								uni.redirectTo({ url: "/pages/enter/index/index"})
							}
						}
						else {
							this.$utils.showErr({ title: res.msg })
						}
					})
					.catch(err => {
						this.$utils.hideLoading()
						this.$utils.showErr()
					})
			}
		},
	}
}
</script>

<style>
</style>
